/**
 * Created by chengshengru on 17-3-12.
 */
//import layer from 'layer';
(function () {
    var CALLBACK_PREFIX = '__LAYER__';

    var CALLBACK_PARAMS = '__LAYER_CALLBACK__';

    var LayerUtils = {


        getParam: function (key, url) {
            url = url || window.location.href;
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)&?", ["i"]);
            var qs = regex.exec(url);
            if (qs == null) {
                return '';
            }
            else {
                return qs[1];
            }
        },
        setParam: function (key, value, url) {
            url = url || window.location.href;
            var regex = new RegExp("([\\?&]" + key + "=)([^&#]*)&?", ["i"]);
            var hash = '';
            if (url.indexOf('#') != -1) {
                url = url.split('#');
                hash = url[1];
                url = url[0];
            }
            var match = regex.exec(url);
            var builder = null;
            if (match == null) {
                if (url.indexOf('?') > 0) {
                    builder = url + "&" + key + "=" + value;
                } else {
                    builder = url + "?" + key + "=" + value;
                }
            }
            else {
                builder = url.replace(regex, '$1' + value + '&');
            }
            if (builder.endsWith('&')) {
                builder = builder.substring(0, builder.length - 1);
            }
            if (hash != '') {
                return builder + '#' + hash;
            }
        },
        /**
         * 序列化参数
         */
        serializeParams: function (url) {
            var search = url || window.location.search;
            if (search.indexOf('?') == 0) {
                search = search.substring(1);
            }
            var params = search.split('&');
            var result = {};
            params.map(function (param) {
                var nvp = param.split('=');
                if (nvp.length == 2) {
                    result[nvp[0]] = nvp[1];
                }
            });
            return result;
        },
        getWindow: function (_window) {
            if (_window) {
                return _window;
            }
            return window.top || window;
        },
        getCallbackName: function () {
            return this.getParam(CALLBACK_PARAMS);
        },
        getDefaultCallback: function (_window) {
            var callback = this.getCallback(this.getCallbackName(), _window);
            return callback;
        },
        bindCallback: function (key, callback, _window) {
            if (key && callback) {
                var __window = this.getWindow(_window);
                __window[CALLBACK_PREFIX + key] = callback;
            }
        },
        getCallback: function (key, _window) {
            var __window = this.getWindow(_window);
            return __window[CALLBACK_PREFIX + key];
        },
        /**
         * 移除回调方法
         * @param key
         * @param _window
         */
        removeCallback: function (key, _window) {
            delete this.getWindow(_window)[CALLBACK_PREFIX + key];
        },
        /**
         * 获取顶层Layer
         *
         * @returns {*}
         */
        getLayer: function (_window) {
            if (_window) {
                return _window.layer;
            }
            return window.top.layer || window.layer;
        },
        toast: function (msg) {
            return this.getLayer().msg(msg || '未知信息');
        },
        open: function (options, callback, _window) {
            options = options || {};
            var that = this;
            var type = options.type;
            var callbackName = null;
            if (2 == type && callback) {
                callbackName = (new Date().getTime());
                //重新设置url
                var content = options.content || '';
                var url = null;
                if (typeof content == 'string') {
                    content = this.setParam(CALLBACK_PARAMS, callbackName, content);
                } else if (typeof content == 'object') {
                    content[0] = this.setParam(CALLBACK_PARAMS, callbackName, content[0]);
                }
                options.content = content;
                this.getWindow(_window)[CALLBACK_PREFIX + callbackName] = callback;
            }
            var end = options.end();
            options.end = function () {
                //处理销毁回调方法操作
                if (callbackName) {
                    delete that.getWindow(_window)[CALLBACK_PREFIX + callbackName];
                }
                end && end();
            }
            return this.getLayer(_window).open(options);
        },
        close: function (index, _window) {
            var _layer = this.getLayer(_window);
            _layer.close(index);
        }
    };
    window.LayerUtils = LayerUtils;
})();
